import React, { Component } from 'react';
import { Button, Form, DatePicker } from 'antd';
import { axios } from 'utils';
import download from "downloadjs";
import {message} from "antd/lib/index";
const {RangePicker} = DatePicker;
const FormItem = Form.Item;

class Forms extends Component {
    constructor(props) {
        super(props);
        this.state = {
            btnDisabled: false,
        };
    }

    submit2 = (e) => {
        e.preventDefault();
        const _this = this;
        const { form: { validateFields, getFieldsValue } } = this.props;
        const {name} = this.props;
        validateFields((err, values) => {
            if(err) {
                return;
            }
            const { time } = getFieldsValue();
            const isArr = Array.isArray(time) && time.length > 0;
            let params = ''
            if(name === '客服导出'){
                params = {
                    yStartTime: isArr ? time[0].format('YYYY-MM-DD 00:00:00') : '',
                    yEndTime: isArr ? time[1].format('YYYY-MM-DD 23:59:59') : ''
                };
            }else{
                params = {
                    startTime: isArr ? time[0].format('YYYY-MM-DD 00:00:00') : '',
                    endTime: isArr ? time[1].format('YYYY-MM-DD 23:59:59') : ''
                };
            }
            _this.exportOrder2(params,time[0].format('YYYY-MM-DD'));
        })
    }

    exportOrder2 = (params,_day) => {
        this.setState({btnDisabled: true});
        const {name, url} = this.props;
        const rename = name.split('导')[0];
        let hide = message.loading('正在导出', 0);
        const _this = this;
        axios({
            url: url,
            method: 'post',
            responseType: 'blob',
            data: params
        }).then((res) => {
            hide && hide();
            _this.setState({btnDisabled: false});
            console.log(1)
            download(res, `${rename}-${_day}.xls`);
        }).catch(() => {
            hide && hide();
            console.log(2)
            _this.setState({btnDisabled: false});
        });
    }

    render() {
        const { form: { getFieldDecorator } } = this.props;
        const { btnDisabled } = this.state;
        return (
            <div>
                  <Form layout="inline" onSubmit={this.submit2}>
                      <FormItem>
                          {
                              getFieldDecorator('time', {
                                  initialValue: this.initTime,
                                  rules: [
                                      { required: true, message: '不能为空' }
                                  ]
                              })(
                                  <RangePicker placeholder={['请选择', '请选择']} format={'YYYY-MM-DD'}/>
                              )
                          }
                      </FormItem>
                      <FormItem>
                          <Button type={'primary'} disabled={btnDisabled} htmlType={'submit'}>{this.props.name}</Button>
                      </FormItem>
                  </Form>
            </div>
        );
    }
}

export default Form.create()(Forms);